var equipmentDetail = {};
var chargingStationData = [];
var equipmentPowerType = [];
var protocolTypeData = [];

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	addTitle("equipmentInfoUpdate");
	search();
    setTimeout(hideLoading, 700);
});

function getProtocolType(data) {
	var length = data.length;
	protocolTypeData = data;
	for (var i = 0; i < length; i++) {
		$("#equipmentType").append("<option value='" + data[i].equipmentType + "'>" + data[i].equipmentNameCN + "</option>");
	}
	$("#equipmentType").val(equipmentDetail.equipmentType);
}
function getChargingStation(data) {
	var length = data.length;
	chargingStationData = data;
	for (var i = 0; i < length; i++) {
		$("#chargingStationID").append("<option value='" + data[i].chargingStationID + "'>" + data[i].chargingStationName + "</option>");
	}
	$("#chargingStationID").val(equipmentDetail.chargingStationID);
}
function getEquipmentType(data) {
	var length = data.length;
	equipmentPowerType = data;
	for (var i = 0; i < length; i++) {
		$("#equipmentPowerType").append("<option value='" + data[i].equipmentPowerType + "'>" + data[i].equipmentPowerTypeName + "</option>");
	}
	$("#equipmentPowerType").val(equipmentDetail.equipmentPowerType);
}
function allClick(tabFlag){
	$(".comBody").hide();
	$("#" + tabFlag).show();
}
function search(){
	$('#EquipmentID').val($("#detailID", parent.document).val());
	$ajax('searchEquipmentDetail', 'searchEquipmentInfoUpdate', searchEquipmentInfoUpdate);
	$("#detailID", parent.document).val("");
}
function searchEquipmentInfoUpdate(data){
	equipmentDetail = data && data.equipmentDetail || {};
	createComBody("detailOneBody", equipmentInfoTable, equipmentDetail, 1);
    createComBody("detailTwoBody", equipmentInfoTable, equipmentDetail, 2);
    $ajaxList('protocolType', getProtocolType);
	$ajaxList('chargingStation', getChargingStation);
	$ajaxList('equipmentType', getEquipmentType);
	$("#equipmentID").attr("readonly", true);
	selectDate("manufactureDate", 0);
	$('#confirm').show();
	$("#currentType").val(equipmentDetail.currentType);
	selectDate("manufactureDate");
	if(equipmentDetail.active == true){
		$("#active").val(1);
	}else{
		$("#active").val(0);
	}
}
function updateEquipmentInfo(data){
     var result = data && data.result;
     if (result == "Success"){
    	 showMessge(messageContent["Update.Success"], undefined, goback);
     } else if (result == "NotFound"){
    	 showMessge(messageContent["Update.Failed"]);
     } else if (result == "EquipmentNameExist"){
         showMessge(messageContent["Equipment.EquipmentNameExist"]);
     } else if (result == "EquipmentNoExist"){
	         showMessge(messageContent["Equipment.EquipmentExist"]);
     } else if (result == "IPandPortExist"){
         showMessge(messageContent["Equipment.EquipmentIPExist"]);
     } else{
    	 showMessge(messageContent["Update.Error"]);
     }
}
function confirm(){
	return checkForm("saveForm", equipmentInfoTable);
}
function goback() {
	window.parent.skipMenu('chargingEquipment/equipmentInfo', 'drownDown2', '', 'equipmentPage', 'equipmentInfo');
}